﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Common.DB;
using QuickDev.Common.Utils;

using QuickDev.Model;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.OPM_ReturnOrderStatus.
    /// </summary>
    public class ReturnOrderStatusDAL
    {
        #region IsExist

        /// <summary>
        /// public static bool IsExist( string name )
        /// </summary>
        /// <param name="name">name</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( string name )
        {
            string procedureName = "OPM_ReturnOrderStatus_IsExist";

            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter()
				};
            parameters[0].Value = name;
            parameters[1].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );
                return ( (int)( parameters[1].Value ) == 0 ? false : true );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }

        #endregion

        #region GetModel

        /// <summary>
        /// public ReturnOrderStatus GetModel( int id )
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>ReturnOrderStatus model</returns>
        public static ReturnOrderStatus GetModel( int id )
        {
            string procedureName = "OPM_ReturnOrderStatus_GetModel";

            ReturnOrderStatus model = new ReturnOrderStatus();

            SqlParameter parameter = new SqlParameter( "@Id" , id );

            try
            {
                ReaderFactory.ReaderToModel<ReturnOrderStatus>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameter ) , model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region GetList

        /// <summary>
        /// public List(ReturnOrderStatus) GetList( int status )
        /// </summary>
        /// <param name="status">status</param>
        /// <returns>List(ReturnOrderStatus)</returns>
        public static List<ReturnOrderStatus> GetList( bool showHidden , bool showDeleted )
        {
            string procedureName = "OPM_ReturnOrderStatus_GetList";

            List<ReturnOrderStatus> modelList = new List<ReturnOrderStatus>();

            SqlParameter[] parameters = {
					new SqlParameter( "@ShowHidden", SqlDbType.Bit, 1 ),
                    new SqlParameter( "@ShowDeleted", SqlDbType.Bit, 1 )
				};
            parameters[0].Value = showHidden;
            parameters[1].Value = showDeleted;

            try
            {
                ReaderFactory.ReaderToList<ReturnOrderStatus>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameters ) , modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region QuickStats

        /// <summary>
        /// public List(ReturnOrderStatus) QuickStats()
        /// </summary>
        /// <returns>List(ReturnOrderStatus)</returns>
        public static List<ReturnOrderStatus> QuickStats()
        {
            string procedureName = "OPM_ReturnOrder_QuickStats";

            List<ReturnOrderStatus> modelList = new List<ReturnOrderStatus>();

            try
            {
                ReaderFactory.ReaderToList<ReturnOrderStatus>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName ) , modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Insert

        /// <summary>
        /// public static int Insert( string name, string description, int displayOrder, bool published, int createdBy )
        /// </summary>
        /// <param name="name">name</param>
        /// <param name="description">description</param>
        /// <param name="displayOrder">displayOrder</param>
        /// <param name="published">published</param>
        /// <param name="createBy">createBy</param>
        /// <returns>Id</returns>
        public static int Insert( string name , string description , int displayOrder , bool published , int createdBy )
        {
            if ( IsExist( name ) )
                throw new ArgumentException
                    ( String.Format( "ReturnOrderStatus {0} is already existed." , name ) );

            string procedureName = "OPM_ReturnOrderStatus_Insert";

            SqlParameter[] parameters = {
				new SqlParameter("@Name", SqlDbType.NVarChar,50),	
				new SqlParameter("@Description", SqlDbType.NVarChar,500),	
				new SqlParameter("@DisplayOrder", SqlDbType.Int,4),	
				new SqlParameter("@Published", SqlDbType.Bit,1),	
				new SqlParameter("@CreatedBy", SqlDbType.Int,4),
                new SqlParameter("@Id", SqlDbType.Int,4)
			};

            int nIndex = 0;
            parameters[nIndex++].Value = name;
            parameters[nIndex++].Value = description;
            parameters[nIndex++].Value = displayOrder;
            parameters[nIndex++].Value = published;
            parameters[nIndex++].Value = createdBy;
            parameters[nIndex].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return (int)Utility.ChangeTypeTo( parameters[nIndex].Value , typeof( Int32 ) );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool Update( int id, string name, string description, int displayOrder, bool published, int updatedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="name">name</param>
        /// <param name="description">description</param>
        /// <param name="displayOrder">displayOrder</param>
        /// <param name="published">published</param>
        /// <param name="updatedBy">updatedBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int id , string name , string description , int displayOrder , bool published , int updatedBy )
        {
            string procedureName = "OPM_ReturnOrderStatus_Update";

            SqlParameter[] parameters = {
				new SqlParameter("@Id", SqlDbType.Int,4),	
				new SqlParameter("@Name", SqlDbType.NVarChar,50),	
				new SqlParameter("@Description", SqlDbType.NVarChar,500),	
				new SqlParameter("@DisplayOrder", SqlDbType.Int,4),	
				new SqlParameter("@Published", SqlDbType.Bit,1),	
				new SqlParameter("@UpdatedBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            int nIndex = 0;
            parameters[nIndex++].Value = id;
            parameters[nIndex++].Value = name;
            parameters[nIndex++].Value = description;
            parameters[nIndex++].Value = displayOrder;
            parameters[nIndex++].Value = published;
            parameters[nIndex++].Value = updatedBy;
            parameters[nIndex].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[nIndex].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Delete

        /// <summary>
        /// public bool Delete( int id, int deletedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="int deletedBy ">int deletedBy </param>
        /// <returns>bool successful</returns>
        public static bool Delete( int id , int deletedBy )
        {
            string procedureName = "OPM_ReturnOrderStatus_Delete";

            SqlParameter[] parameters = {
                new SqlParameter("@Id", SqlDbType.Int, 4),
                new SqlParameter("@DeletedBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = id;
            parameters[1].Value = deletedBy;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[2].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion
    }
}

